package meterMonitor.controller;

import com.alibaba.fastjson.JSONObject;
import meterMonitor.biz.IntegrateBiz;
import meterMonitor.biz.MeterBiz;
import meterMonitor.biz.MonitorBiz;
import meterMonitor.consts.ResponseCode;
import meterMonitor.framework.auth.AuthorityType;
import meterMonitor.framework.auth.FireAuthority;
import meterMonitor.mapper.AreaMapper;
import meterMonitor.mapper.MonitorMapper;
import meterMonitor.util.BizUtil;
import meterMonitor.vo.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.text.DecimalFormat;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2019/1/16 0016.
 * 综合---Controller
 */
@RequestMapping("integrate")
@RestController
public class IntegrateController extends BaseController {
    public static final Logger logger = LoggerFactory.getLogger(IntegrateController.class);
    @Autowired
    IntegrateBiz integrateBiz;
    @Autowired
    MeterBiz meterBiz;
    @Autowired
    MonitorBiz monitorBiz;
    @Autowired
    AreaMapper areaMapper;
    @Autowired
    MonitorMapper monitorMapper;
    @Autowired
    BizUtil bizUtil;

    @RequestMapping("integrateIndex.do")
    @FireAuthority(authorityTypes = AuthorityType.Query)
    private ModelAndView index(ModelAndView mv) {
        return mv;
    }

    /**
     * yy:根据类型查询本月设备累计用量(总个数)
     * @return
     */

    @RequestMapping("queryTodayByDevice.do")
    @ResponseBody
    @FireAuthority(authorityTypes = AuthorityType.Query)
    private BaseResponseData queryTodayByDevice(@RequestBody(required=true) String deviceTypeCode) {
        long startTime=System.currentTimeMillis(); //获取开始时间

        BaseResponseData response = new BaseResponseData();
        DecimalFormat df = new DecimalFormat("#0.00");
//        String str1=deviceTypeCode.substring(19,20);
//        @RequestBody(required=true) String deviceTypeCode

        String str1=deviceTypeCode.substring(19,20);
        int type=Integer.parseInt(str1);
        try{
            TodayWater model=new TodayWater();
            model=integrateBiz.queryTodayByDevice(type);
            TodayWater todayWater1 = integrateBiz.queryTodayByDevice(1);
            TodayWater todayWater0 = integrateBiz.queryTodayByDevice(0);


            model.setAvg(Double.valueOf(df.format(todayWater1.getAvg())));
            model.setMonthStandardWater(todayWater1.getMonthStandardWater());
            model.setmExcessiveWater(todayWater1.getmExcessiveWater());
            model.setMonthStandardWater1(todayWater1.getSss());
            model.setAvg1(Double.valueOf(df.format((todayWater0.getAvg()))));
            model.setMonthStandadElectricity(todayWater0.getMonthStandardWater());
            model.setmExcessiveElectricity(todayWater0.getmExcessiveWater());
            model.setMonthStandadElectricity1(todayWater0.getSss());


            response.setData(model);
            response.setCode(ResponseCode.SUCCESS);
        }catch (Exception ex){
            logger.error("查询综合数据异常!", ex);
            response.setCode(ResponseCode.DEFAULT_EXCEPTION);
            response.setMessage("系统异常！请联系管理员");
        }
        long endTime=System.currentTimeMillis(); //获取结束时间
        System.out.println("changxu_Start_time： "+(endTime-startTime)+"ms");
        return response;

    }


    /**
     * 本月超标水表情况
     *
     * @return
     */
    @RequestMapping("getsuperMeter.do")
    @ResponseBody
    @FireAuthority(authorityTypes = AuthorityType.Query)
    public BaseResponseData getsuperMeter(@RequestBody(required=true) String deviceTypeCode) {
        BaseResponseData response = new BaseResponseData();
        String str1=deviceTypeCode.substring(19,20);
        int type=Integer.parseInt(str1);
        try {
                MeterOverMonthVo overVo = integrateBiz.queryCurrentMonth(type);
                response.setData(overVo);
                response.setCode(ResponseCode.SUCCESS);
           } catch (Exception e) {
                logger.error("查询当月超标情况异常！", e);
                response.setCode(ResponseCode.DEFAULT_EXCEPTION);
                response.setMessage("查询当月超标情况异常！");
        }
//
        return response;
    }


    /**
     * 每月用水总量统计
     *
     * @return
     */
    @RequestMapping("getMeterGyMonth.do")
    @ResponseBody
    @FireAuthority(authorityTypes = AuthorityType.Query)
    public BaseResponseData getMeterGyMonth(@RequestBody(required=true) String deviceTypeCode) {

        BaseResponseData response = new BaseResponseData();

        String str1=deviceTypeCode.substring(19,20);
        int type=Integer.parseInt(str1);

        if (str1.equals("1")) {
            try {
                List<MonitorMeter> monitorMonths = integrateBiz.queryMonthWater();
                response.setData(monitorMonths);
                response.setCode(ResponseCode.SUCCESS);
            } catch (Exception e) {
                logger.error("查询月用水总量统计异常！", e);
                response.setCode(ResponseCode.DEFAULT_EXCEPTION);
                response.setMessage("查询月用水总量统计！");
            }
        }else if(str1.equals("0")){
            try {
                List<MonitorMeter> monitorMonths = integrateBiz.queryMonthElectricity();
                response.setData(monitorMonths);
                response.setCode(ResponseCode.SUCCESS);
            } catch (Exception e) {
                logger.error("查询月用水总量统计异常！", e);
                response.setCode(ResponseCode.DEFAULT_EXCEPTION);
                response.setMessage("查询月用水总量统计！");
            }
        }

        return response;
    }






}
